package tools;
/**
 * Filename:    getProcess.java
 *
 * @author: yyi
 * @version: 1.0
 * @date: 2018-12-3
 * @Description:
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class getProcess {
    public static boolean findProcess(String processName) {
        BufferedReader bufferedReader = null;
        try {
            Process proc = Runtime.getRuntime().exec("cmd /c tasklist /fi " + "\"imagename eq ReliabilityDASS.exe\"");
            bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(processName)) {
                    return true;
                }
            }
            return false;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    public static String subString(String str, String strStart, String strEnd) {
        int strStartIndex = str.indexOf(strStart);
        int strEndIndex = str.indexOf(strEnd);
        if (strStartIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strStart + ", 无法截取目标字符串";
        }
        if (strEndIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strEnd + ", 无法截取目标字符串";
        }
        String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length());
        return result;
    }
    public static String getPID() {
        String pid = null;
        String cmd = "tasklist /nh /FI \"IMAGENAME eq ReliabilityDASS.exe\"";
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(cmd);
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = null;
            while((line=br.readLine()) != null){
                if(line.indexOf("ReliabilityDASS.exe") != -1){
                    String lineWithOutSpace = line.replace(" ","");
                    pid = subString(lineWithOutSpace,"ReliabilityDASS.exe","Console");
                    return pid;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return pid;
    }

    public static String executeCmd(String command) throws IOException {
        System.out.println("Execute command : " + command);
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("cmd /c " + command);
       /* BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
        String line = null;
        StringBuilder build = new StringBuilder();
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            build.append(line);
        }
        return build.toString();*/
       return null;
    }




}

